package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;
import model.Supplier;

public class AddRemoveTab extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4363485966939917318L;
	
	private Manager manager;
	private JTextField nameText, addText, cityText, statusText, removeText;
	
	private JButton addSup;
	private JButton removeSup;
	private JPanel main;
	private JTextArea textArea;
	private JScrollPane confirmation;
	
	public AddRemoveTab(Manager manager){
		this.manager = manager;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		init();
		
	}
	
	public void init(){
		main = new JPanel();
		initAdd();
		initRemove();
		initConfirm();
		add(main);
		
	}
	
	private void initAdd(){
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(5, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		adder.add(new JLabel("Name: "));
        nameText = new JTextField();
        nameText.setPreferredSize(new Dimension(300, 30));
        adder.add(nameText);
        
        adder.add(new JLabel("Address: "));
        addText = new JTextField();
        addText.setPreferredSize(new Dimension(300, 30));
        adder.add(addText);
        
		adder.add(new JLabel("City: "));
		cityText = new JTextField();
		cityText.setPreferredSize(new Dimension(300, 30));
		adder.add(cityText);
		
		adder.add(new JLabel("Status(Active/Inactive): "));
		statusText = new JTextField();
		statusText.setPreferredSize(new Dimension(300, 30));
		adder.add(statusText);
		
		adder.add(new JLabel("Click to add supplier"));
		
		addSup = new JButton("Add Supplier");
		addSup.addActionListener(new addSuppliers());
		adder.add(addSup);
		
		add(adder);
	}
	
	private void initConfirm(){
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Add Supplier Confirmation");
		conPanel.setBorder(title);
		
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        confirmation = new JScrollPane(textArea);
        confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        conPanel.add(confirmation, BorderLayout.CENTER);
		
        add(conPanel);
	}
	
	private void initRemove(){
		JPanel remove = new JPanel();
		/*JTextArea textArea = new JTextArea(20, 1);
		JScrollPane scrollPane = new JScrollPane(textArea);*/
		
		remove.setLayout(new GridLayout(2, 2));
		remove.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		remove.add(new JLabel("Please enter name of supplier to be removed: "));
		removeText = new JTextField();
		removeText.setPreferredSize(new Dimension(300, 30));
		remove.add(removeText);
		//remove.add(scrollPane, BorderLayout.CENTER);
		
		remove.add(new JLabel("Click to remove supplier"));
		removeSup = new JButton("Remove Supplier");
		removeSup.addActionListener(new removeSuppliers());
		remove.add(removeSup);
		
		add(remove);
	}
	
	public class removeSuppliers implements ActionListener{
		
		String name;
		
		public void actionPerformed(ActionEvent e) {
			name = removeText.getText();
			if(name.isEmpty()){
				JOptionPane.showMessageDialog(addSup,
					    "Please check that the following have been entered:\n- Name",
					    "Deletion Error",
					    JOptionPane.ERROR_MESSAGE);
			}
			else{
				try{
					manager.removeSupplier(name);
				}catch (SQLException e1) {
					JOptionPane.showMessageDialog(addSup,
						    "The supplier does not exists.\nPlease retry.",
						    "Delete Supplier Error",
						    JOptionPane.ERROR_MESSAGE);
					System.out.println("A problem occurred while adding supplier");
			}
			}
		}
		
	}
	public class addSuppliers implements ActionListener{

		String name, address, city, status;
		
		public void actionPerformed(ActionEvent e) {
			name = nameText.getText();
			address = addText.getText();
			city = cityText.getText();
			status = statusText.getText();

			
			if (name.isEmpty() || address.isEmpty() || city.isEmpty() || status.isEmpty()){
				JOptionPane.showMessageDialog(addSup,
					    "Please check that the following have been entered:\n- Name \n- Address\n- City\n- Status",
					    "Add Supplier Error",
					    JOptionPane.ERROR_MESSAGE);
			}
			else{
				try{
					Supplier S = new Supplier(name, address, city, status);
					manager.addSupplier(S);
					textArea.append("******************* Supplier Added ******************" + "\n" +
							"\n" + "Name: " + name + 
							"\n" + "Address: " + address + 
							"\n" + "City: " + city + 
							"\n" + "Status: " + status + 
							"\n\n" + 
							"*********************************************************" + "\n\n");
				}
				catch (SQLException e1) {
					JOptionPane.showMessageDialog(addSup,
						    "The supplier already exists.\nPlease retry.",
						    "Add Supplier Error",
						    JOptionPane.ERROR_MESSAGE);
					System.out.println("A problem occurred while adding supplier");
			}
		}
	}
}
}
